/*
 * @Author: 杨上漂
 * @Date: 2024-10-13 10:27:42
 * @LastEditors: 杨上漂
 * @LastEditTime: 2024-10-16 10:38:54
 * @FilePath: \gwlc\assets\scripts\StarlightBullet.ts
 * @Description: 
 * 
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. 
 */
import { _decorator, BoxCollider2D, Collider, Collider2D, Component, Contact2DType, Director, director, ITriggerEvent, log, Node, TERRAIN_SOUTH_INDEX, v2, v3 } from 'cc';
import { EnemyMgr } from '../mainScene/EnemyMgr';
import { gameMgr } from '../mainScene/gameMgr';
import { Player } from '../mainScene/Player';
import { playerSkillTypeEnum } from '../Enum';
import { Enemy } from '../mainScene/Enemy';
import { lookAtTarget } from '../utils/utils';
import { PoolMgr } from '../PoolMgr';

const { ccclass, property } = _decorator;

@ccclass('StarlightBullet')
export class StarlightBullet extends Component {

    attackTargetPos=null;

    tempPos=v3();

    init() {
        this.poolReset();
        //获取目标敌人
        this.attackTargetPos = EnemyMgr.ins.getNearlyExistOneEnemyNodeNottTornado().position;

        let collider = this.node.getComponent(BoxCollider2D);
        collider.on(Contact2DType.BEGIN_CONTACT, this.onTriggerEnterFuc, this);

        lookAtTarget(this.attackTargetPos,this.node,30);
        gameMgr.ins.targetLineMove(this.attackTargetPos,this.node,Player.ins.starlightSpeed)

    }
    poolReset() {//重新设置，针对预制体池
        this.attackTargetPos = null;
        this.tempPos = v3();
    }


    onTriggerEnterFuc(self: Collider, other: Collider) {
        if (other.node.getComponent(BoxCollider2D).tag==1) {
            director.once(Director.EVENT_AFTER_PHYSICS,()=>{
                other.node.getComponent(Enemy).hurt(playerSkillTypeEnum.starLightBullet);
                if (!Player.ins.starLightIsCanThrough) {//如果不可以穿透就销毁
                    console.log("不能穿透销毁")
                this.recycle();
                }
            },this)
        }
}

    recycle(){
        // this.node.destroy();
        PoolMgr.ins.poolPut(this.node.name,this.node);
    }
 

    // protected update(dt: number): void {
    //     if (!gameMgr.ins.isGamePause) {
    //         if (Math.abs(this.node.position.x)>600||Math.abs(this.node.position.y)>1000) {//超出界面销毁
    //             this.recycle();
    //         }

    //         if (!Player.ins.starLightIsCanThrough) {
    //         gameMgr.ins.trackTargetMove(this.attackTargetPos, this.node, Player.ins.starlightSpeed, dt)
                
    //         }
    //         else{
    //             gameMgr.ins.trackTargetMove(v2(this.attackTargetPos.x*3,this.attackTargetPos.y*3), this.node, Player.ins.starlightSpeed, dt)
    //         }

    //     }
    // }

 
    
 
}


